﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using DAL = AccessDAL;
using CommonFunc;
using DBUtility;

namespace BLL
{
    public class Link
    {
        DAL.LinkTableAdapters.LinkTableAdapter DL =
            new DAL.LinkTableAdapters.LinkTableAdapter();

        MODEL.Link _ML = new MODEL.Link();

        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="ML"></param>
        /// <returns></returns>
        public MODEL.Link GetDataByID(MODEL.Link ML)
        {
            try
            {
                DataTable DT = DL.GetDataByID(ML.Link_ID);

                _ML.Link_DateTime = Convert.ToDateTime(DT.Rows[0]["Link_DateTime"]);
                _ML.Link_ID = Convert.ToInt32(DT.Rows[0]["Link_ID"]);
                _ML.Link_Image = DT.Rows[0]["Link_Image"].ToString();
                _ML.Link_State = Convert.ToBoolean(DT.Rows[0]["Link_State"]);
                _ML.Link_Title = DT.Rows[0]["Link_Title"].ToString();
                _ML.Link_URL = DT.Rows[0]["Link_URL"].ToString();

                return _ML;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加链接
        /// <summary>
        /// 添加链接
        /// </summary>
        /// <param name="ML"></param>
        public void Add(MODEL.Link ML)
        {
            try
            {
                DataTable DT_ByURL = DL.GetDataByURL(ML.Link_URL);
                if (DT_ByURL.Rows.Count != 0)
                {
                    throw new Exception("此链接地址已添加过...");
                }

                DL.Add(ML.Link_Title, ML.Link_Image, ML.Link_URL, ML.Link_State, ML.Link_DateTime);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改连接
        /// <summary>
        /// 修改连接
        /// </summary>
        /// <param name="ML"></param>
        public void Mod(MODEL.Link ML)
        {
            try
            {
                DL.Mod(ML.Link_Title, ML.Link_URL, ML.Link_State, ML.Link_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除链接
        /// <summary>
        /// 删除链接
        /// </summary>
        /// <param name="ML"></param>
        public void Del(MODEL.Link ML)
        {
            try
            {
                _ML = GetDataByID(ML);

                string LinkImage = _ML.Link_Image;

                DL.Del(ML.Link_ID);


                if (LinkImage != "")
                {
                    CommonFunction.DelFile(
                        HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + _ML.Link_Image);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 切换链接状态
        /// <summary>
        /// 切换链接状态
        /// </summary>
        /// <param name="ML"></param>
        public void ModState(MODEL.Link ML)
        {
            try
            {
                string SQLstr_ModState = "UPDATE [Link] SET [Link_State]=NOT [Link_State] ";
                SQLstr_ModState += "WHERE [Link_ID]=" + ML.Link_ID;

                AccHelper.ExecuteSQL(SQLstr_ModState);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改链接图片
        /// <summary>
        /// 修改链接图片
        /// </summary>
        /// <param name="ML"></param>
        public void ModLinkImage(MODEL.Link ML)
        {
            try
            {
                _ML = GetDataByID(ML);

                string OldImage = _ML.Link_Image;

                DL.ModLinkImage(ML.Link_Image, ML.Link_ID);

                CommonFunction.DelFile(
                    HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + OldImage);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
